package com.bishe.tax.controller.sys;

import com.bishe.tax.entity.SysUser;
import com.bishe.tax.service.SysDeptService;
import com.bishe.tax.service.SysRoleService;
import com.bishe.tax.service.SysUserService;
import com.bishe.tax.utils.AjaxResult;
import com.bishe.tax.utils.StringUtils;
import com.bishe.tax.utils.TableDataInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/user")
public class SysUserController {
    private String prefix = "sysUser";

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysDeptService sysDeptService;

    @GetMapping("/list")
    public String user() {
        return prefix + "/list";
    }


    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUser user,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> list = userService.selectUserList(user);
        return TableDataInfo.returnDataTable(new PageInfo<>(list).getTotal(), list);
    }


    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("roles", roleService.selectRoleAll());
        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysUser user, HttpSession session) {
        SysUser sysUser = (SysUser) session.getAttribute("sysUser");
        if (StringUtils.isNotNull(user.getUserId()) && SysUser.isAdmin(user.getUserId())) {
            return AjaxResult.success("不允许修改超级管理员用户");
        }
        user.setCreateBy(sysUser.getLoginName());
        return AjaxResult.success( userService.insertUser(user));
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap) {
        mmap.put("user", userService.selectUserById(userId));
        mmap.put("roles", roleService.selectRolesByUserId(userId));
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysUser user, HttpSession session) {
        SysUser sysUser = (SysUser) session.getAttribute("sysUser");
        if (StringUtils.isNotNull(user.getUserId()) && SysUser.isAdmin(user.getUserId())) {
            return AjaxResult.error("不允许修改超级管理员用户");
        }
        return AjaxResult.success( userService.updateUser(user));
    }


    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return AjaxResult.success("", userService.deleteUserByIds(ids));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 校验用户名
     */
    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public String checkLoginNameUnique(SysUser user) {
        return userService.checkLoginNameUnique(user.getLoginName());
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(SysUser user) {
        return userService.checkPhoneUnique(user);
    }

    /**
     * 校验email邮箱
     */
    @PostMapping("/checkEmailUnique")
    @ResponseBody
    public String checkEmailUnique(SysUser user) {
        return userService.checkEmailUnique(user);
    }

    /**
     * 用户状态修改
     */
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysUser user) {
        return userService.changeStatus(user);
    }


    @GetMapping("/edit/{deptId}/{userId}")
    @ResponseBody
    public AjaxResult edit(@PathVariable("deptId") Long deptId, @PathVariable("userId") Long userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user", userService.selectUserById(userId));
        map.put("roles", roleService.selectRolesByUserId(userId));
        map.put("deptTree", sysDeptService.selectDeptById(deptId));
        return AjaxResult.success( map);
    }


}